<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Member domain model
 *
 * @category    RedCross
 * @package     Core_Model
 */
class Core_Model_Member extends RedCross_Gateway_Base_Member
{

	public function init()
	{
        $this->_shiftExchangeService = $this->_helper->getService('Core_Service_ShiftExchange');
	}

	public function isActive() {
		return is_null($this->quit_date);
	}

	public function getId() {
		return (int)$this->id;
	}

	/**
	 * Returns the main email address of the member.
	 * Returns the first occurence of emails if no email is set as main
	 * Returns false if the member has no emails registered
	 * @return unknown|unknown|string
	 */
	public function getMainEmail()
	{
		$emails = $this->emails;
		foreach ($emails as $email)
		{
			if ((bool)$email->is_main)
			{
				$emailModel = new Core_Model_Email();
				$emailModel->fromArray($email->toArray());
				return $emailModel;
			}
		}
		return false;
	}

	/**
	 * Returns an array with emails of the member as Core_Model_Email, not the
	 * base classes Doctrine produces. Use this if you need special methods only
	 * present in Core_Model_Email.
	 * @return array An array of Core_Model_Email
	 */
	public function getEmails()
	{
		$emails = $this->emails;
		if (count($emails) > 0)
		{
			$emailModels = array();
			foreach ($emails as $email)
			{
				$emailModel = new Core_Model_Email();
				$emailModel->fromArray($email->toArray());
				$emailModels[] = $emailModel;
			}
		} else
		{
			$emailModels = false;
		}
		return $emailModels;
	}

	/**
	 * Returns the permanent passive message for the member, if any
     *
	 * @return Core_Model_PermanentPassive|boolean The permanent passive message or false if this does not exist
	 */
	public function getPermanentPassiveMessage()
	{
        $ppMessage = $this->PermanentPassiveMessage;
		return $ppMessage;
	}

	/**
	 *
	 */
	public function getPrimaryPhone()
	{
		$ret = null;

		if (!is_null($this->Phonenumbers))
			$ret = $this->Phonenumbers->getFirst();
		return $ret;
	}

	/* Untested */
	public function hasPassiveMessageOnDate(Zend_Date $date)
	{
		$format = 'YYYYMMdd';
		foreach ($this->PassiveMessages as $message)
		{
			$messageDate = new RedCross_Date($message->date,RedCross_Date::DB_FMT);
			if ($messageDate->get($format) == $date->get($format))
			{
				return true;
			}
		}
		return false;
	}

	/* Untested */
	public function getFullName()
	{
		return $this->first_name . ' ' . $this->last_name;
	}

    public function getPostalCode()
    {
        if(!is_null($this->postal_id)) {
            return (int) $this->postal_id;
        }
        return $this->postal_id;
    }

    public function getPostalPlace()
    {
        if(!is_null($this->postal_id)) {
            return $this->Postal->area;
        }
        return;
    }
}
